package com.sumainfor.zuul.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.util.UUIDUtils;
import com.sumainfor.zuul.entity.SysDictEntity;
import com.sumainfor.zuul.service.SysDictService;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author PuaChen
 * @Create 2019-01-12 16:36
 */
@RestController
@RequestMapping("/sys/dict")
public class SysDictController extends AbstractController {

    @Autowired
    private SysDictService dictService;

    @RequestMapping("/list")
    public JsonResult selectDictList(Page page, @RequestParam Map<String, Object> params) {
        String key = MapUtils.getString(params, "key");
        EntityWrapper<SysDictEntity> wrapper = new EntityWrapper<>();
        wrapper.like(StringUtils.isNotBlank(key), "label", key);
        wrapper.or();
        wrapper.like(StringUtils.isNotBlank(key), "type", key);
        wrapper.or();
        wrapper.like(StringUtils.isNotBlank(key), "description", key);
        wrapper.orderBy("instm", false);
        Page response = dictService.selectPage(page, wrapper);
        return JsonResult.success(response);
    }


    /**
     * 根据ID 去查询记录
     *
     * @param id
     * @return
     */
    @RequestMapping("/info")
    public JsonResult getInfoById(Integer id) {
        if (id == null) {
            return JsonResult.errorForEmpty();
        }
        SysDictEntity entity = dictService.selectById(id);
        if (entity == null) {
            return JsonResult.error("没有找到这条记录");
        }
        return JsonResult.success(entity);
    }


    @PostMapping("/save")
    public JsonResult save(@RequestBody SysDictEntity entity) {
        if (entity == null) {
            return JsonResult.errorForEmpty();
        }
        entity.setId(null);
        entity.setDictId(UUIDUtils.generatePrefixUuid("Dict"));
        entity.setInstm(new Date());
        entity.setDelfg(0);
        boolean state = dictService.insert(entity);
        return state ? JsonResult.actionSuccess() : JsonResult.actionFailure();
    }

    @PostMapping("/update")
    public JsonResult update(@RequestBody SysDictEntity entity) {
        if (entity == null) {
            return JsonResult.errorForEmpty();
        }
        entity.setUpdtm(new Date());
        entity.setUpdby(getUserId() + "");
        boolean state = dictService.updateById(entity);
        return state ? JsonResult.actionSuccess() : JsonResult.actionFailure();
    }


    @PostMapping("delete")
    public JsonResult delete(@RequestBody List<Integer> ids) {
        if (ids == null || ids.isEmpty()) {
            return JsonResult.errorForEmpty();
        }
        boolean state = dictService.deleteBatchIds(ids);
        return state ? JsonResult.actionSuccess() : JsonResult.actionFailure();
    }

}
